package handler

import (
	"database-platform/handler/common"
	"database-platform/logger"
	"database-platform/model"
	"database-platform/service"
	"fmt"
	"github.com/gin-gonic/gin"
)

// ListDatabaseHandler 处理获取数据库列表的请求
func ListDatabaseHandler(c *gin.Context) {
	params, err := getDatabaseListParams(c)
	if err != nil {
		msg := fmt.Sprintf("ListDatabaseHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	res, err := service.GetDatabaseList(params)
	if err != nil {
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithData(c, res)
}

func getDatabaseListParams(c *gin.Context) (params *model.DatabaseListRequest, err error) {
	params = &model.DatabaseListRequest{
		DBType:       c.DefaultQuery("db_type", ""),
		PhysicalName: c.DefaultQuery("physical_name", ""),
		LogicalName:  c.DefaultQuery("logical_name", ""),
		ClusterID:    common.ParseInt(c.DefaultQuery("cluster_id", "")),
		Env:          c.DefaultQuery("env", ""),
		IsShard:      common.ParseBool(c.DefaultQuery("is_shard", "false")),
		AppName:      c.DefaultQuery("app_name", ""),
		BusinessOne:  c.DefaultQuery("business_one", ""),
		BusinessTwo:  c.DefaultQuery("business_two", ""),
		Status:       c.DefaultQuery("status", ""),
		Search:       c.DefaultQuery("search", ""),
		ShowCluster:  common.ParseBool(c.DefaultQuery("show_cluster", "false")),
	}
	params.Page, params.PageSize = common.GetPagePageSizeParams(c)
	return params, err
}

func RetrieveDatabaseHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("RetrieveDatabaseHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}
	result, err := service.GetDatabaseById(id)
	if err != nil {
		msg := fmt.Sprintf("RetrieveDatabaseHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, fmt.Sprintf("获取详情错误，id:%v, err:%s", id, err.Error()))
		return
	}
	common.OkWithData(c, result)
}

// InsertDatabaseHandler 处理插入数据库记录的请求
func InsertDatabaseHandler(c *gin.Context) {

	var database model.Database
	if err := c.ShouldBindJSON(&database); err != nil {
		logger.Logger.Error(fmt.Sprintf("InsertDatabaseHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	err := service.InsertDatabase(&database)
	if err != nil {
		msg := fmt.Sprintf("InsertDatabaseHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "插入成功")
}

// UpdateDatabaseHandler 处理更新数据库记录的请求
func UpdateDatabaseHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateDatabaseHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}

	var database model.Database
	if err := c.ShouldBindJSON(&database); err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateDatabaseHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	err = service.UpdateDatabase(id, &database)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateDatabaseHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "更新成功")
}

// DeleteDatabaseHandler 处理删除数据库记录的请求
func DeleteDatabaseHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("DeleteDatabaseHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}

	err = service.DeleteDatabase(id)
	if err != nil {
		msg := fmt.Sprintf("DeleteDatabaseHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "删除成功")
}
